const pattern = /(?=((?!\b)\d{3})+$)/g
// 在install里面添加要用的组件
function install (Vue) {
  Vue.filter('fixed2', function (v) {
    if (v === null || v === undefined) {
      return ''
    }
    return +Number.parseFloat(v).toFixed(2)
  })

  Vue.filter('fixed4', function (v) {
    if (v === null || v === undefined) {
      return ''
    }
    return +Number.parseFloat(v).toFixed(4)
  })

  Vue.filter('money', function (v, precision = 2) {
    if (v === null || v === undefined) {
      return ''
    }
    const number = Number(String(v).replace(/,/g, '')).toFixed(precision)
    const arr = number.split('.')
    arr[0] = arr[0].replace(pattern, ',')
    return arr.join('.')
  })

  Vue.filter('percent', function (v) {
    if (v === null || v === undefined) {
      return ''
    }
    return Number(v) * 100 + '%'
  })
}

export default {
  install
}
